//
//  AppDelegate.m
//  HA
//
//  Created by sy on 2023/12/16.
//

#import "AppDelegate.h"
#import "HATabBarViewController.h"
#import "HANavViewController.h"
#import "HAUpdatePopView.h"
#import "HALoginVC.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


static id appDelegate;

+ (AppDelegate* )single {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    });
    return appDelegate;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    ///
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;

    IQKeyboardManager *keyboardManager = [IQKeyboardManager sharedManager]; // 获取类库的单例变量
    [keyboardManager registerTextFieldViewClass:[YYTextView class] didBeginEditingNotificationName:YYTextViewTextDidBeginEditingNotification didEndEditingNotificationName:YYTextViewTextDidEndEditingNotification];
    keyboardManager.enable = YES; // 控制整个功能是否启用
    keyboardManager.shouldResignOnTouchOutside = YES; // 控制点击背景是否收起键盘
    keyboardManager.enableAutoToolbar = NO;
    keyboardManager.keyboardDistanceFromTextField = 10.0f; // 输入框距离键盘的距离

    if (![NSString isEmptyString:HAUserDefaults.token] && ![NSString isEmptyString:HAUserDefaults.userID]) {
        self.window.rootViewController = [[HATabBarViewController alloc]init];

    }else {
        self.window.rootViewController = [[HANavViewController alloc]initWithRootViewController:[HALoginVC new]];
    }
    
    [self.window makeKeyWindow];
    [self.window makeKeyAndVisible];
    
    [self haappversion];

    return YES;
}



-(void)haappversion {
    SYRequest *request = [[SYRequest alloc]init];
    request.requestMethod = SYRequestMethodGET;
    request.requestUrl = @"common/version";
    [[SYNetworking shareSingle] hgj_asynRequestWithArray:@[request] responseBlock:^(SYDataModel *dataModel) {
        if (dataModel.request == request) {
            if (dataModel.code == 1) {
                if ([[dataModel.data allKeys]containsObject:@"ios_buildNumber"]) {
                    NSInteger ios_buildNumber = [[dataModel.data valueForKey:@"ios_buildNumber"] integerValue];
                    NSInteger build = [[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"] integerValue];
                    if (ios_buildNumber > build) {
                        HAUpdatePopView *menu = [[HAUpdatePopView alloc]init];
                        menu.ios_url = [NSString stringWithFormat:@"%@",[dataModel.data valueForKey:@"ios_url"]];
                        [menu show];
                    }
                }
            }
        }
    }];
}



@end
